const app = getApp()
var xrSdk     = require('../../../vendor/hope-sdk/index.js');
var xrRequest = require('../../../vendor/hope-sdk/core/request.js');
var config = require('../../../config.js');
const util = require('../../../utils/util.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    webURL: config.service.imageUrl,
    position: '',
    figure_desc: [],
    tyrewidthList  : [],
    flatratioList  : [],
    diameterList: [],
    loadindexList: [],
    speedlevelList: [],

    tyrewidth: '',
    flatratio: '',
    diameter: '',
    loadindex: '',
    speedlevel: '',

    typeIndex: -1,
    typeName: '请选择',

    //上传地址
    serverUrl: config.service.upload,
    // 1.条码图
    im1: '',
    // 2.花纹图
    im2: '',
    // 3.花纹名称图
    im3: '',
    // 4.规格图
    im4: '',
    // 5.DOT图
    im5: '',
    dot:''
  },
  result: function (e) {
    console.log(e)
    var that = this;
    var imgArr  = e.detail.imgArr;
    if (imgArr != null && imgArr.length > 0){
      let img = imgArr[0];
      if (img.indexOf("wxfile://tmp") == 0) {
        return;
      }
      let type = e.currentTarget.dataset.type;
      switch (type) {
        case "1":
          that.setData({
            im1: img
          });
          break;
        case "2":
          that.setData({
            im2: img
          });
          break;
        case "3":
          that.setData({
            im3: img
          });
          break;
        case "4":
          that.setData({
            im4: img
          });
          break;
        case "5":
          that.setData({
            im5: img
          });
          break;
      }
    }
  },
  previewImage: function (e) {
    let url = e.currentTarget.dataset.url;
    wx.previewImage({
      current: 0,
      urls: [url]
    })
  },
  remove: function (e) {
    var that = this;
    //移除图片
    console.log(e)
    //let index = e.detail.index;
    var type = e.currentTarget.dataset.type;
    switch(type){
      case "1": 
        that.setData({
          im1:""
        });
       break;
      case "2":
        that.setData({
          im2: ""
        });
        break;
      case "3":
        that.setData({
          im3: ""
        });
        break;
      case "4":
        that.setData({
          im4: ""
        });
        break;
      case "5":
        that.setData({
          im5: ""
        });
        break;
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    that.setData({
      position: options.position
    });
    that.initConfig();
  },
  initConfig:function(){
    var that = this;
    xrRequest.request(app,{
      url: config.service.tyreConfig,
      login: false,
      success: function (res) {
        if (res.data.code == 1) {
          that.setData({
            figure_desc: res.data.data.figure_desc,
            tyrewidthList: res.data.data.tyrewidth,
            flatratioList: res.data.data.flatratio,
            diameterList: res.data.data.diameter,
            loadindexList: res.data.data.loadindex,
            speedlevelList: res.data.data.speedlevel,

            tyrewidth: res.data.data.tyrewidth[0],
            flatratio: res.data.data.flatratio[0],
            diameter: res.data.data.diameter[0],
            loadindex: res.data.data.loadindex[0],
            speedlevel: res.data.data.speedlevel[0]
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          })
        }
      },
      fail: function (res) { },
    })
  },
  /**
   * 提交订单
   */
  submitForm:function(e){
    var that = this;
    if (that.data.typeName == '' || that.data.typeName == '请选择') {
      wx.showToast({
        title: '轮胎花纹不能为空',
        icon: 'none',
      })
      return;
    }
    var tyre_desc = that.data.tyrewidth.fullname + "/" + that.data.flatratio.fullname + "R" + that.data.diameter.fullname + " " + that.data.loadindex.fullname + that.data.speedlevel.fullname;  
    console.log(tyre_desc);

    if (that.data.im1 == "" || that.data.im1 == null){
      wx.showToast({
        title: '条码图未上传',
        icon: 'none',
      })
      return;
    }
    if (that.data.im2 == "" || that.data.im2 == null) {
      wx.showToast({
        title: '花纹图未上传',
        icon: 'none',
      })
      return;
    }
    if (that.data.im3 == "" || that.data.im3 == null) {
      wx.showToast({
        title: '花纹名称图未上传',
        icon: 'none',
      })
      return;
    }
    if (that.data.im4 == "" || that.data.im4 == null) {
      wx.showToast({
        title: '规格图未上传',
        icon: 'none',
      })
      return;
    }
    if (that.data.im5 == "" || that.data.im5 == null) {
      wx.showToast({
        title: 'DOT图未上传',
        icon: 'none',
      })
      return;
    }
    if (that.data.dot == "" || that.data.dot == null){
      wx.showToast({
        title: 'DOT不能为空',
        icon: 'none',
      })
      return;
    }
    var tyre = {
      'barcode': '-1',                     // 手动输入描述的条码是空
      'figure_desc': that.data.typeName,   // 花纹
      'tyre_desc': tyre_desc,              // 规格
      'dot': that.data.dot,                // DOT
      'img1': that.data.im1,
      'img2': that.data.im2,
      'img3': that.data.im3,
      'img4': that.data.im4,
      'img5': that.data.im5
    };
    switch (that.data.position) {
      case 'lf': xrSdk.cookie.set('l_f_tire', tyre); break;
      case 'rf': xrSdk.cookie.set('r_f_tire', tyre); break;
      case 'le': xrSdk.cookie.set('l_e_tire', tyre); break;
      case 're': xrSdk.cookie.set('r_e_tire', tyre); break;
    }
    wx.navigateBack({
      delta: 1
    });
  },
  bindFigureDesc: function (e) {
    var that = this;
    console.log('picker发送选择改变，携带值为', e.detail.value)
    that.setData({
      typeIndex: e.detail.value,
      typeName: that.data.figure_desc[e.detail.value].fullname
    });
  },
  bindChange:function(e){
    console.log(e);
    const val = e.detail.value;
    this.setData({
      tyrewidth: this.data.tyrewidthList[val[0]],
      flatratio: this.data.flatratioList[val[2]],
      diameter: this.data.diameterList[val[4]],
      loadindex: this.data.loadindexList[val[5]],
      speedlevel: this.data.speedlevelList[val[6]]
    })
  },
  dotInput:function(e){
    this.setData({
      dot: e.detail.value
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})